var express = require('express');
var router = express.Router();

let movieService = require("../service/movieService.js");
let ResultUtil = require("../util/ResultUtil.js");





// 增加
router.post("/addMovie", async function (req, res) {
    let movie = req.body;
    // console.log(movie);
    let result = await movieService.addMovie(movie);
    res.send(result);
})

// 删除
router.delete("/delMovie/:id", async function (req, res) {
    let id = req.params.id;
    let result = await movieService.deleteMovie(id);
    res.send(result);

})

// 更新
router.post("/updateMovie", async function (req, res) {
    let movie = req.body;
   
   
    let result = await movieService.updateMovie(movie);
    

    if (result) {
        res.send({ code: 200, msg: "更新成功" })
       
    } else {
        res.send({ code: -1, msg: "更新失败" })
    }
})






// 分页显示查询,查询有数组的查询不到
router.get("/listMovie", async function (req, res) {
    console.log("显示页面");
    
    let { key, val, page, limit } = req.query;
    let filter = null;
    if (key != null) {
        if (key === "score") {
            filter = { "score": val };
            // 如果input框是没有值则全部显示
            if (!val) {
                filter = null;
            }
        }
        if (key === "type") {
            filter = { "type": new RegExp(val) };
            if (!val) {
                filter = null;
            }
        }
        if (key === "area") {
            filter = { "area": val };
            if (!val) {
                filter = null;
            }
        }
       
       
    };
    let result = await movieService.list(filter, page, limit);
    res.send(result);


})








module.exports = router;